package com.dxy.chatserver.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.dxy.chatserver.db.entity.AcceptLineMsgResponse;
import com.dxy.chatserver.db.entity.EsOnlineMessageBuild;
import com.dxy.chatserver.mapper.ChatMsgMapper;
import com.dxy.chatserver.service.EsService;
import com.dxy.chatserver.vo.ReqFindPageReqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/msg")
@Slf4j
public class EsController {

    @Autowired
    private EsService esService;
    @Autowired
    private ChatMsgMapper msgMapper;

    @GetMapping("/count")
    public Long count() throws IOException {
        return esService.count();
    }
    @GetMapping("/test")
    public Long test() throws IOException {
//        esService.delIndex();
        esService.indexCreate();
        esService.indexExist();
//        List<AcceptLineMsgResponse> acceptLineMsgResponses = msgMapper.find("[group]343");
//        esService.addBatch(acceptLineMsgResponses);

//        ReqFindPageReqVo reqVo =new ReqFindPageReqVo();
//        reqVo.setSession("[group]338");
//        reqVo.setStartTime(1655177769065L);
//        reqVo.setEndTime(1655264404604L);
//        reqVo.setPage(1);
//        reqVo.setSize(20);
//        reqVo.setSort(0);
//        List<EsOnlineMessageBuild> page = esService.findPage(reqVo);
        ReqFindPageReqVo reqFindPageReqVo = new ReqFindPageReqVo();
        reqFindPageReqVo.setSession("[group]363");
        reqFindPageReqVo.setSize(20);
        reqFindPageReqVo.setPage(1);
        reqFindPageReqVo.setStartTime(1577880268606L);
        reqFindPageReqVo.setEndTime(1655294668606L);
        esService.findNativeSearch(reqFindPageReqVo);
        return null;
    }
    @RequestMapping("/findPage")
    public List<AcceptLineMsgResponse> findPage(@RequestBody String request){
        log.debug("接口请求参数{}",request);
        if(ObjectUtil.isEmpty(request)){
            return null;
        }
        ReqFindPageReqVo reqFindPageReqVo = JSONUtil.toBean(request, ReqFindPageReqVo.class);
        if(ObjectUtil.isEmpty(reqFindPageReqVo.getEndTime()) || ObjectUtil.isEmpty(reqFindPageReqVo.getStartTime()) || ObjectUtil.isEmpty(reqFindPageReqVo.getSession())){
            return null;
        }
        List<EsOnlineMessageBuild> page = esService.findPage(reqFindPageReqVo);
        if(page==null){
            return null;
        }
        return AcceptLineMsgResponse.build(page);
//        return JSONUtil.toJsonStr(page);
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("session","[group]223");
        jsonObject.set("startTime",1577881768924L);
        jsonObject.set("endTime",1655296168924L);
        jsonObject.set("page",1);
        jsonObject.set("size",30);
        jsonObject.set("sort",1);
        String s = HttpUtil.post("http://47.108.134.250:10590/msg/findPage", jsonObject.toString());
        List<AcceptLineMsgResponse> acceptLineMsgResponses1 = JSON.parseArray(s, AcceptLineMsgResponse.class);
        System.out.println(acceptLineMsgResponses1.size());
    }
}
